package com.haojiang.mh.remote.adapter

import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.bean.ClientBedBean
import com.haojiang.mh.remote.utils.BleUtil.Companion.groupData
import com.haojiang.mh.remote.utils.Util.Companion.playSound
import com.haojiang.mh.remote.widget.ClientBedItemView
import kotlinx.android.synthetic.main.item_client_bed.view.*
import kotlinx.android.synthetic.main.item_client_bed.view.cardView
import kotlinx.android.synthetic.main.item_group_type.view.tvName
import java.util.*

class ClientBedDialogFragAdapter :
    RecyclerView.Adapter<ClientBedDialogFragAdapter.ViewHolder>() {

    private var dataList = arrayListOf<ClientBedBean>(
//        ClientBedBean(true, "BTX4", "VJRM", R.drawable.btx4),
//        ClientBedBean(false, "BT2000LP", "VJRM", R.drawable.bt2000lp),
//        ClientBedBean(false, "BT3000LP", "UFRM", R.drawable.bt2000lp),
//        ClientBedBean(false, "BT4000LP", "VCRM", R.drawable.bt2000lp)
    )

    private val viewList = ArrayList<ClientBedItemView>()
    private var grayColor: Int = 0
    private var whiteColor: Int = 0
    private var bgSelectColor = 0
    private var bgUnselectColor = 0

    init {
        grayColor = ContextCompat.getColor(AppContext.instance(), R.color.color_gray_dark)
        whiteColor = ContextCompat.getColor(AppContext.instance(), R.color.color_white)
        bgSelectColor = ContextCompat.getColor(AppContext.instance(), R.color.color_black_light3)
        bgUnselectColor = ContextCompat.getColor(AppContext.instance(), R.color.color_black_light2)
        groupData.groupKey.interfaceNo = dataList.get(0).interfaceNo
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(ClientBedItemView(parent.context))       //BleItemView是layout文件
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemView = holder.itemView as ClientBedItemView
        if (!viewList.contains(itemView)) {
            viewList.add(itemView)
            val clientBedBean = dataList.get(position)
            itemView.clientBedBean = clientBedBean
            itemView.ivBed.setImageResource(clientBedBean.imgRes)
            itemView.tvName.setText(clientBedBean.interfaceName)
            setTextColor(itemView)
            itemView.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    playSound()
                    viewList.forEach {
                        if(it.equals(v)) {
                            it.clientBedBean!!.isSelect = true
                            groupData.groupKey.interfaceNo = it.clientBedBean!!.interfaceNo
                        }else {
                            it.clientBedBean!!.isSelect = false
                        }
//                        it.clientBedBean!!.isSelect = v!!.equals(it)
                        setTextColor(it)
                    }
                }
            })
        }
    }

    private fun setTextColor(itemView: ClientBedItemView) {
        if (itemView.clientBedBean!!.isSelect) {
            itemView.tvName.setTextColor(whiteColor)
            itemView.cardView.setCardBackgroundColor(bgSelectColor)
        } else {
            itemView.tvName.setTextColor(grayColor)
            itemView.cardView.setCardBackgroundColor(bgUnselectColor)
        }
    }


//    private fun setBg(position: Int) {
//        viewList.forEach {
//            val groupTyp = it.tag
//            when (groupTyp) {
//                GROUP_TYPE_SINGLE -> {
//                    if (it.equals(viewList.get(position))) {
//                        it.ivIcon.setImageResource(R.drawable.ic_group_single_p)
//                    } else {
//                        it.ivIcon.setImageResource(R.drawable.ic_group_single_n)
//                    }
//                }
//                GROUP_TYPE_MULTIPLE -> {
//                    if (it.equals(viewList.get(position))) {
//                        it.ivIcon.setImageResource(R.drawable.ic_group_multiple_p)
//                    } else {
//                        it.ivIcon.setImageResource(R.drawable.ic_group_multiple_n)
//                    }
//                }
//                GROUP_TYPE_SPLIT -> {
//                    if (it.equals(viewList.get(position))) {
//                        it.ivIcon.setImageResource(R.drawable.ic_group_split_p)
//                    } else {
//                        it.ivIcon.setImageResource(R.drawable.ic_group_split_n)
//                    }
//                }
//            }
////            it.tvName.setTextColor(grayColor)
//        }
////        viewList.get(position).tvName.setTextColor(whiteColor)
//    }

    fun getSelectInterfaceNo(): String {
        viewList.forEach {
            if (it.clientBedBean!!.isSelect) {
                return it.clientBedBean!!.interfaceNo
            }
        }
        return ""
    }

//    private fun clickItemDelay(view: View, delayMs: Int) {
//        view.isEnabled = false
//        view.postDelayed({ view.isEnabled = true }, delayMs.toLong())
//    }

//    fun dataRefresh() {
//        Collections.sort(dataList)
//        notifyDataSetChanged()
//    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }
}